package me.wangqu.weiyouqu.web.rest.business.dao.impl;

import me.wangqu.weiyouqu.web.rest.business.dao.JpushDao;
import me.wangqu.weiyouqu.web.rest.business.model.BaseModel;
import me.wangqu.weiyouqu.web.rest.util.UrlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by joln on 2016/7/25.
 */
@Repository
public class JpushImpl implements JpushDao {

    private final Logger log = LoggerFactory.getLogger(JpushImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public BaseModel getJpushData(Integer id) {
        StringBuffer sql = new StringBuffer(128);
        sql.append("select w.id,w.code ,w.type,w.title,w.day as date,w.pic,l.link,i.type from article w,link l,imgtype i where i.flag=? and w.id=?");
        List<Object> params = new ArrayList<>();
        params.add("jpush");
        params.add(id);
        return jdbcTemplate.queryForObject(sql.toString(), params.toArray(), new RowMapper<BaseModel>() {
            @Override
            public BaseModel mapRow(ResultSet rs, int rowNum) throws SQLException {
                BaseModel bases = new BaseModel();
                bases.setTitle(rs.getString("title"));
                String link = rs.getString("link");
                bases.setUrl(UrlUtil.getUrl(link, rs.getString("date"), rs.getInt("id"), rs.getInt("w.type"), rs.getInt("code")));
                bases.setDate(rs.getString("date"));
                bases.setImg_arr(rs.getString("pic"));
                return bases;
            }
        });

    }
}
